package com.barbeque.mall.controller.mallmanagement;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.keyword.MarketKeyword;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.service.mallmanagement.KeywordService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ycc
 * @date 2022/07/15 19:58:26
 */
@RestController
@RequestMapping("admin/keyword")
public class KeywordController {

    @Autowired
    KeywordService keywordService;

    //GET http://localhost:8083/admin/keyword/list?page=1&limit=20&sort=add_time&order=desc
    @RequiresPermissions("admin:keyword:list")
    @GetMapping("list")
    public BaseRespVo list(BasePageInfo pageInfo,MarketKeyword keyword){
        //TODO:查询关键词信息
        CommonData<MarketKeyword> list = keywordService.list(pageInfo,keyword);
        return BaseRespVo.ok(list);
    }

    //POST http://localhost:8083/admin/keyword/create
    @RequiresPermissions("admin:keyword:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody MarketKeyword keyword){
        if (StringUtils.isEmpty(keyword.getUrl())) {
            return BaseRespVo.invalidData("内容不能为空");
        }
        int affectedRows = keywordService.create(keyword);
        //未添加成功提示错误信息
        if (affectedRows==0) {
            return BaseRespVo.invalidData();
        }
        return BaseRespVo.ok(keyword);
    }

    //POST http://localhost:8083/admin/keyword/update
    @RequiresPermissions("admin:keyword:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketKeyword keyword){
        int affectedRows = keywordService.update(keyword);
        if (affectedRows==0) {
            return BaseRespVo.invalidData();
        }
        return BaseRespVo.ok(keyword);
    }

    //POST http://localhost:8083/admin/keyword/delete
    @RequiresPermissions("admin:keyword:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketKeyword keyword){
        int affectedRows = keywordService.delete(keyword);
        //未删除成功提示错误信息
        if (affectedRows==0) {
            return BaseRespVo.invalidData();
        }
        return BaseRespVo.ok("ok");
    }
}
